package com.wholesale.utils;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.wholesale.R;
import com.wholesale.beans.UserAddress;

public class MyAddressListAdapter extends ArrayAdapter<UserAddress> {

	private ListView listView;
	private int selected = 0;
	private boolean showSelect = false;

	public MyAddressListAdapter(Context context, ListView listView) {
		super(context, 0);
		this.listView = listView;
	}

	public MyAddressListAdapter(Context context,
			List<UserAddress> UserAddressrList, ListView listView) {
		super(context, 0, UserAddressrList);
		this.listView = listView;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) getContext();

		// Inflate the views from XML
		View rowView = convertView;
		UserAddressCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.address, null);
			viewCache = new UserAddressCache(rowView);
			rowView.setTag(viewCache);
		} else {
			viewCache = (UserAddressCache) rowView.getTag();
		}
		UserAddress userAddress = getItem(position);
		viewCache.getFullNameView().setText(userAddress.getConsignee());
		viewCache.getPhoneView().setText(userAddress.getMobile());
		viewCache.getCountryNameView().setText(userAddress.getCountryName());
		viewCache.getProviceNameView().setText(userAddress.getProvinceName());
		viewCache.getCityNameView().setText(userAddress.getCityName());
		viewCache.getDetailAddressView().setText(userAddress.getAddress());
		viewCache.getPostalCodeView().setText("" + userAddress.getZipcode());
		if (showSelect) {
			if (selected == position) {
				rowView.findViewById(R.id.selectedBtn).setVisibility(
						View.VISIBLE);
			} else {
				rowView.findViewById(R.id.selectedBtn).setVisibility(
						View.INVISIBLE);
			}
		} else {
			rowView.findViewById(R.id.selectedBtn).setVisibility(View.GONE);
		}

		return rowView;
	}

	public void setShowSelect(boolean select) {
		this.showSelect = select;
	}

	public void setSelected(int index) {
		this.selected = index;
	}

	public void addItems(List<UserAddress> items) {
		for (UserAddress userAddress : items) {
			this.add(userAddress);
		}
	}

}
